// lib/views/settings_view.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/settings_controller.dart';

class SettingsView extends StatelessWidget {
  final SettingsController controller = Get.find();

  SettingsView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('配置'),
        backgroundColor: Colors.blue[700],
        foregroundColor: Colors.white,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '股票数据管理',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            Card(
              elevation: 3,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      '导入股票代码',
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 8),
                    const Text(
                      '从Baostock获取所有股票代码并保存到本地数据库',
                      style: TextStyle(color: Colors.grey),
                    ),
                    const SizedBox(height: 16),
                    Obx(() => Center(
                      child: ElevatedButton(
                        onPressed: controller.isImporting.value
                            ? null
                            : () => controller.importStockCodes(),
                        child: controller.isImporting.value
                            ? const SizedBox(
                          width: 20,
                          height: 20,
                          child: CircularProgressIndicator(strokeWidth: 2),
                        )
                            : const Text('导入股票数据'),
                      ),
                    )),
                    const SizedBox(height: 16),
                    // 进度条
                    Obx(() {
                      if (controller.isImporting.value) {
                        return Column(
                          children: [
                            LinearProgressIndicator(
                              value: controller.importProgress.value,
                              backgroundColor: Colors.grey[200],
                              valueColor: const AlwaysStoppedAnimation<Color>(Colors.blue),
                            ),
                            const SizedBox(height: 8),
                            Text(
                              '${(controller.importProgress.value * 100).toStringAsFixed(1)}%',
                              style: const TextStyle(fontSize: 12),
                            ),
                          ],
                        );
                      }
                      return const SizedBox();
                    }),
                    const SizedBox(height: 16),
                    Obx(() {
                      if (controller.importStatus.value.isNotEmpty) {
                        return Container(
                          padding: const EdgeInsets.all(12),
                          decoration: BoxDecoration(
                            color: controller.importStatus.value.contains('成功')
                                ? Colors.green[50]
                                : controller.importStatus.value.contains('失败')
                                ? Colors.red[50]
                                : Colors.blue[50],
                            borderRadius: BorderRadius.circular(8),
                            border: Border.all(
                              color: controller.importStatus.value.contains('成功')
                                  ? Colors.green
                                  : controller.importStatus.value.contains('失败')
                                  ? Colors.red
                                  : Colors.blue,
                            ),
                          ),
                          child: Text(
                            controller.importStatus.value,
                            style: TextStyle(
                              color: controller.importStatus.value.contains('成功')
                                  ? Colors.green[800]
                                  : controller.importStatus.value.contains('失败')
                                  ? Colors.red[800]
                                  : Colors.blue[800],
                            ),
                          ),
                        );
                      }
                      return const SizedBox();
                    }),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 24),
            Card(
              elevation: 3,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      '数据库管理',
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 8),
                    const Text(
                      '管理本地数据库中的股票数据',
                      style: TextStyle(color: Colors.grey),
                    ),
                    const SizedBox(height: 16),
                    Row(
                      children: [
                        ElevatedButton(
                          onPressed: controller.isImporting.value
                              ? null
                              : () => controller.clearDatabase(),
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.red[50],
                            foregroundColor: Colors.red,
                          ),
                          child: const Text('清空数据库'),
                        ),
                        const SizedBox(width: 16),
                        Obx(() => Text(
                          '当前股票数量: ${controller.stockCount.value}',
                          style: const TextStyle(fontSize: 14),
                        )),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}